//
//  ZHeaderViewController.m
//  顶部视图+放大
//
//  Created by 周成龙 on 17/2/15.
//  Copyright © 2017年 ZCL. All rights reserved.
//

#import "ZHeaderViewController.h"

#import "HMObjcSugar.h"
#import "YYWebImage.h"

#define kHeaderHeight 200
NSString *const ID = @"cell";
@interface ZHeaderViewController () <UITableViewDelegate, UITableViewDataSource>
{
    /// 顶部视图view
    UIView              *_headView;
    /// 顶部视图图片
    UIImageView         *_headImageView;
    /// 导航线条
    UIView              *_lineView;
    /// 状态栏的状态
    UIStatusBarStyle     _status;
}
@end

@implementation ZHeaderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _status = UIStatusBarStyleLightContent;
    
    [self setupUI];
    [self setupHeaderImage];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.navigationController setNavigationBarHidden:YES];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return _status;
}

- (void)setupUI {
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    tableView.contentInset = UIEdgeInsetsMake(kHeaderHeight, 0, 0, 0);
    tableView.scrollIndicatorInsets = UIEdgeInsetsMake(kHeaderHeight, 0, 0, 0);
    
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}

- (void)setupHeaderImage {
    // http://img07.tooopen.com/images/20170215/tooopen_sy_198728087187.jpg
    
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.hm_width, kHeaderHeight)];
    _headView.backgroundColor = [UIColor hm_colorWithHex:0xF8F8F8];
    [self.view addSubview:_headView];
    
    _headImageView = [[UIImageView alloc] initWithFrame:_headView.bounds];
    _headImageView.backgroundColor = [UIColor hm_colorWithHex:0x000033];
    [_headView addSubview:_headImageView];
    _headImageView.contentMode = UIViewContentModeScaleAspectFill;
    _headImageView.clipsToBounds = YES;
    
    CGFloat height = 1 / [UIScreen mainScreen].scale;
    _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, kHeaderHeight - height, _headView.hm_width, height)];
    _lineView.backgroundColor = [UIColor lightGrayColor];
    [_headView addSubview:_lineView];
    
    NSURL *url = [NSURL URLWithString:@"http://img07.tooopen.com/images/20170215/tooopen_sy_198728087187.jpg"];
    [_headImageView yy_setImageWithURL:url options:1];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    cell.textLabel.text = @(indexPath.row).stringValue;
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offset = scrollView.contentOffset.y + scrollView.contentInset.top;
    if (offset <= 0) {
        // 放大
        _headView.hm_y = 0;
        _headView.hm_height = kHeaderHeight - offset;
    }else {
        // 整体移动
        _headView.hm_height = kHeaderHeight;
        
        // 固定位置
        CGFloat min = kHeaderHeight - 64;
        _headView.hm_y = -MIN(min, offset);//-offset;
        
        // 透明度
        CGFloat alpha = 1 - offset / min;
        _headImageView.alpha = alpha;
        
        // 更新状态栏的状态
        _status = (alpha < 0.5) ? UIStatusBarStyleDefault : UIStatusBarStyleLightContent;
        [self.navigationController setNeedsStatusBarAppearanceUpdate];
    }
    // 图片的高度
    _headImageView.hm_height = _headView.hm_height;
    // 导航线条的位置
    _lineView.hm_y = _headView.hm_height - _lineView.hm_height;
}

@end
